package util

import (
	"bytes"
	"encoding/json"
	"github.com/sirupsen/logrus"
	"strconv"
	//"fmt"
	"math/big"
	"strings"
)

func JsonData(obj interface{}) string {
	byteBuf := bytes.NewBuffer([]byte{})
	encoder := json.NewEncoder(byteBuf)
	encoder.SetEscapeHTML(false)
	err := encoder.Encode(obj)
	if err != nil {
		panic(err)
	}
	str := byteBuf.String()
	str = strings.Replace(str, "\\n", "", -1)
	str = strings.Replace(str, "\\r", "", -1)
	return str
}

/*base代表进制，如传入10代表10进制*/
func StrToBigint(str string, base int) *big.Int {
	i := new(big.Int)
	i.SetString(str, base)
	return i

}

func FloatToBigInt(val float64) *big.Int {
	bigval := new(big.Float)
	bigval.SetFloat64(val)
	// Set precision if required.
	// bigval.SetPrec(64)
	coin := new(big.Float)
	coin.SetInt(big.NewInt(1000000000000000000))
	bigval.Mul(bigval, coin)
	result := new(big.Int)
	bigval.Int(result) // store converted number in result
	return result
}

func StringToFloat64(str string) (float64, error) {
	f, err := strconv.ParseFloat(str, 64)
	if nil != err {
		logrus.Error(err)
		return 0, err
	}
	return f, nil
}

//func GetDay(t1 time.Time) time.Time {
//	year, month, day := t1.Date()
//	var cstSh, _ = time.LoadLocation("America/Los_Angeles") //上海
//	date := time.Date(year, month, day, 0, 0, 0, 0, cstSh)
//	return date
//}

//func BigintToString(b big.Int) string {
//
//	return b.String()
//}
